/**
 * Created by Bin_Shi on 2016/11/17.
 */
//设定画布宽高
var _canvas = document.getElementById('main');
_canvas.width = window.innerWidth;
_canvas.height = window.innerHeight;
//离屏canvas
var canvas = document.createElement('canvas');
canvas.width = _canvas.width;
canvas.height = _canvas.height;
//用户ID
var ID = '';
var player = '';
//其他玩家数组
var others = [];
//速度
var v = 10;
//碰撞范围
var force_radius = 50;
//接触碰撞速度基数
var force_v = 0.25;
//最小碰撞速度
var min_force_v = 5;
//边界宽度
var border_width = 30;
//鼠标按下标记
var click = false;
//遮罩层宽高设定
$('#overlay').css('width', window.innerWidth);
$('#overlay').css('height', window.innerHeight);
//WebSocket对象
var ws = getWebSocket();
var ready = false;

function start(){
    var overlay = document.getElementById('overlay');
    overlay.style.display = 'none';
    var name = document.getElementById('name');
    player = initPlayer(ID, name.value);
}

//总流程
function animate() {
    if(ready){
        var ctx = _canvas.getContext('2d');
        ctx.clearRect(0,0,_canvas.width,_canvas.height);
        drawMap(canvas);
        var flag = checkObstacles(obstacles);
        if(flag){
            death();
            return;
        }
        var collision = checkCollision();
        if(collision.length != 0){
            collisionMove(collision, v);
        }
        else{
            move();
        }
        if(player){
            drawPlayer(canvas, player, true);
        }
        drawOthers(canvas, others);
        if(player && player.alive){
            wsSend(ws, player);
        }
        ctx.drawImage(canvas, 0, 0);
    }
    window.requestAnimationFrame(animate);
}

function replay(){
    player = initPlayer(ID, player.name);
    others = [];
    requestAnimationFrame(animate);
    $('#overlay').hide();
    $('#replay').hide();
}


